package com.yanqu.road.server.manager.activity;

import com.yanqu.road.entity.activity.ActivityDisperseGoldInfo;
import com.yanqu.road.logic.bussiness.cross.ActivityDisperseGoldBussiness;
import com.yanqu.road.server.TempMgr;

import java.util.Map;
import java.util.concurrent.atomic.AtomicLong;

/**
 * 活动送财
 */
public class ActivityDisperseGoldMgr extends TempMgr {


    private static Map<Long, ActivityDisperseGoldInfo> activityDisperseGoldInfoMap;

    /**
     * 送财最大id
     */
    private static AtomicLong disperseId;
    @Override
    public boolean reloadData() throws Exception {
        activityDisperseGoldInfoMap = ActivityDisperseGoldBussiness.getCrossDisperseGoldInfoMap();
        disperseId = new AtomicLong(ActivityDisperseGoldBussiness.getMaxDisperseId());
        return true;
    }

    @Override
    public boolean save() {
        for(ActivityDisperseGoldInfo goldInfo : activityDisperseGoldInfoMap.values()){
            if(goldInfo.isInsertOption()){
                ActivityDisperseGoldBussiness.addCrossActivityDisperseGoldInfo(goldInfo);
            }else{
                ActivityDisperseGoldBussiness.updateCrossActivityDisperseGoldInfo(goldInfo);
            }
        }
        return true;
    }

    @Override
    public boolean init() throws Exception {
        return reload();
    }

    @Override
    public boolean reloadConfig() throws Exception {
        return true;
    }

    @Override
    public boolean stop() {
        return false;
    }

    public static void  addActivityDisperseGoldInfo(ActivityDisperseGoldInfo activityDisperseGoldInfo){
        activityDisperseGoldInfo.setDisperseId(disperseId.incrementAndGet());
        activityDisperseGoldInfo.setInsertOption();
        activityDisperseGoldInfoMap.put(activityDisperseGoldInfo.getDisperseId(), activityDisperseGoldInfo);
    }

    public static ActivityDisperseGoldInfo getActivityDisperseGoldInfo(long disperseId){
        return activityDisperseGoldInfoMap.get(disperseId);
    }
}
